﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Config;
using NLog.Targets.Wrappers;
using NLog.Web;
using System.Reflection;
using XYS.Core.Models;

namespace XYS.Service.Core
{
    /// <summary>
    /// ELK日志 相关配置
    /// </summary>
    public static class ELKExtension
    {
        /// <summary>
        /// 添加ELK日志
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="loggerFactory"></param>
        /// <returns></returns>
        public static WebApplicationBuilder AddELK(this WebApplicationBuilder builder, ILoggerFactory loggerFactory)
        {
            var logger = loggerFactory.CreateLogger("XYS.Core");
            //获取配置
            var keylist = builder.Configuration.AsEnumerable().Select(x => x.Key).Distinct().Order().ToList();
            var enable = builder.Configuration.GetValue<bool?>("ELKSetting:Enable");
            if (enable == true)
            {
                logger.Log(Microsoft.Extensions.Logging.LogLevel.Information, $"已开启 ELK 存储Index名字:{builder.Configuration["ELKSetting:IndexName"]}");
                // NLog: Setup NLog for Dependency injection
                builder.Host.UseNLog();
            }
            else logger.Log(Microsoft.Extensions.Logging.LogLevel.Warning, "您尚未配置 ELK，控制台日志将不会被记录");
            return builder;
        }

    }
}
